class Solution {
public:
    string removeKdigits(string num, int k) {
        if(k>=num.size())
        return "0";
        vector<int> v;
        for (auto e : num) {
            while (v.size() > 0 && v.back() > e-'0' && k) {
                v.pop_back();
                k -= 1;
            }
            v.push_back(e - '0');
        }
        for (; k > 0; --k) 
        v.pop_back();

        while(v[0]==0)
        v.erase(v.begin());

        string tmp;
        for(auto e:v)
        {
            tmp+=(e+'0');
        }
        return tmp;
    }
};
